package myservlet.control;

import mybean.data.super_manager_Bean;
import mybean.data.shopContent_Bean;

import java.io.*;
import java.sql.*;
import java.text.MessageFormat;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/superDShop")
public class super_d_shop extends HttpServlet {
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        super_manager_Bean managerBean = null;
        //管理员信息的bean
        try {
            managerBean = (super_manager_Bean) session.getAttribute("superLoginBean");
            if (managerBean == null) {
                managerBean = new super_manager_Bean();  //创建Javabean对象
                session.setAttribute("superLoginBean", managerBean);
            }
        } catch (Exception exp) {
            managerBean = new super_manager_Bean();
            session.setAttribute("superLoginBean", managerBean);
        }

        //判断当前是否是管理员登录状态
        if (managerBean.getUsername().equals("") || managerBean.getPassword().equals("")) {
            fail(request, response, "请您先登录管理员账号再删除普通用户");
            //没有登录, 不给删除
            return;
        }




        request.setCharacterEncoding("gb2312");
        String dataBase = "web_used";
        String tableName = request.getParameter("shopType");
        String TDsid = request.getParameter("sid");


        if (tableName == null || tableName.length() == 0) {
            fail(request, response, "未填写要删除的商品类别，请您重新输入");
            return;
        }

        if (TDsid == null || TDsid.length() == 0) {
            fail(request, response, "未填写要删除的商品id，请您重新输入");
            return;
        }

        long l_sid = Long.parseLong(TDsid);
        if(l_sid >  Integer.MAX_VALUE || l_sid < 0){
            fail(request, response, "商品id输入有误");
            //有误下面的都不用做了
            return;
        }
        int int_sid = (int)l_sid;


        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
        }


        String condition = MessageFormat.format(
                "DELETE FROM {0} where sid = {1}",
                tableName, int_sid);

//        DELETE FROM web_used.costume WHERE sid = 2
        Connection con;
        Statement sql;
        ResultSet rs;

        try {
            String uri = "jdbc:mysql://127.0.0.1/" + dataBase + "?" +
                    "user=root&password=root&characterEncoding=gb2312";
            con = DriverManager.getConnection(uri);
            // 判断一下是否唯一

//            String only = "select * from acount where username = '"+username+ "'";
            String only = MessageFormat.format(
                    "select * from {0} " +
                            "where sid = {1}",
                    tableName, int_sid
            );
            sql=con.createStatement();
            ResultSet only_rs =sql.executeQuery(only);
            boolean m= only_rs.next();
            if(m == false){
                fail(request, response, "此商品不存在哦");
            } else {
                try {
                    sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);
                    sql.executeUpdate(condition);
                    success(request, response, "删除指定商品成功!");
                } catch (SQLException e) {
                    System.out.println(e);
                    fail(request, response, "删除商品失败:" + e.toString());
                }
            }
//            RequestDispatcher dispatcher = request.getRequestDispatcher("/member/add_success.jsp");
//            dispatcher.forward(request, response);

        } catch (SQLException e) {
            System.out.println("删除商品失败");
            fail(request, response, "删除商品失败:" + e.toString());
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void success(HttpServletRequest request, HttpServletResponse response,
                        String backNews) {
        response.setContentType("text/html;charset=GB2312");
        try {
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><style>body{text-align: center;background: #ea66a6;margin:300px}</style></head>");
            out.println("<body>");
            out.println("<h2>" + backNews + "</h2>");
            out.println("返回");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/system_operate/delete_u_s.jsp>删除用户,商品页面</a>");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/system_operate/super_index.jsp>管理员功能主页</a>");
            out.println("</body></html>");
        } catch (IOException exp) {
        }
    }

    public void fail(HttpServletRequest request, HttpServletResponse response,
                     String backNews) {
        response.setContentType("text/html;charset=GB2312");
        try {
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><style>body{text-align: center;background: #ea66a6;margin:300px}</style></head>");
            out.println("<body>");
            out.println("<h2>" + backNews + "</h2>");
            out.println("返回");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/system_operate/super_index.jsp>管理员功能主页</a>");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/system_operate/super_login.jsp>管理员登录页面</a>");
            out.println("</body></html>");
        } catch (IOException exp) {
        }
    }
}